package com.suyang.syt.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suyang.syt.auth.AuthClass;
import com.suyang.syt.auth.AuthMethod;
import com.suyang.syt.model.User;
import com.suyang.syt.model.UserGroup;
import com.suyang.syt.model.UserRole;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.PagerEasyUI;
import com.suyang.syt.model.dto.UserDTO;
import com.suyang.syt.model.dto.UserGroupDTO;
import com.suyang.syt.model.dto.UserRoleDTO;
import com.suyang.syt.service.interfaces.UserGroupService;
import com.suyang.syt.service.interfaces.UserRoleService;
import com.suyang.syt.service.interfaces.UserService;
import com.suyang.syt.util.DateFormat;
import com.suyang.syt.util.EmptyUtil;
import com.suyang.syt.util.StringUtil;

@Controller
@RequestMapping("/admin/user")
@AuthClass("login")
public class AdminUserController {
	@Autowired
	private UserService userService;
	@Autowired
	private UserGroupService userGroupService;
	@Autowired
	private UserRoleService userRoleService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "user/list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public PagerEasyUI<UserDTO> list(HttpServletRequest request, int page, int rows, String sort, String order, User user) {
		try {
			String createDate1 = request.getParameter("createDate1");
			String createDate2 = request.getParameter("createDate2");
			return userService.listByEasyUI(page, rows, sort, order, user, createDate1, createDate2);
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/findAll", method = RequestMethod.POST)
	@ResponseBody
	public PagerEasyUI<UserDTO> findAll(HttpServletRequest request, int page, int rows, String sort, String order) {
		try {
			return userService.listByEasyUI(page, rows, sort, order, null, null, null);
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj add(HttpServletRequest request, UserDTO userDTO) {
		try {
			User user = (User) request.getSession().getAttribute("loginUser");
			userDTO.setCreateName(user.getUsername());
			userDTO.setCreateTime(DateFormat.dateToString(new Date()));
			return userService.add(userDTO);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/deletes", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj deletes(HttpServletRequest request, String ids) {
		try {
			return userService.deletes(ids);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	@AuthMethod(role = "ROLE_JGJS")
	public AjaxObj update(HttpServletRequest request, UserDTO userDTO) {
		try {
			User user = (User) request.getSession().getAttribute("loginUser");
			userDTO.setModifyName(user.getUsername());
			userDTO.setModifyTime(DateFormat.dateToString(new Date()));
			return userService.update(userDTO);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	/** 单个登陆用户改密 */
	@RequestMapping(value = "/updatePwd", method = RequestMethod.POST)
	@ResponseBody
	@AuthMethod
	public AjaxObj updatePwd(HttpServletRequest request, String password, String passwordNew, String passwordRe) {
		try {
			User user = (User) request.getSession().getAttribute("loginUser");
			return userService.updatePwd(user, password, passwordNew, passwordRe);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	/** 批量重置密码 */
	@RequestMapping(value = "/updateAllPwd", method = RequestMethod.POST)
	@ResponseBody
	@AuthMethod(role = "ROLE_JGJS")
	public AjaxObj updateAllPwd(HttpServletRequest request, String ids, String names) throws Exception {
		try {
			return userService.updateAllPwd(ids, names);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@AuthMethod(role = "ROLE_PXJG")
	@RequestMapping(value = "/startUse", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj updateStartUse(HttpServletRequest request, String ids, String names) throws Exception {
		try {
			return userService.updateUserStart(ids, names);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@AuthMethod(role = "ROLE_PXJG")
	@RequestMapping(value = "/stopUse", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj updateStopUse(HttpServletRequest request, String ids, String names) throws Exception {
		try {
			return userService.updateUserStop(ids, names);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/listGroupUser/{groupId}", method = RequestMethod.POST)
	@ResponseBody
	public List<User> listGroupUser(HttpServletRequest request, @PathVariable long groupId) {
		try {
			return userService.listGroupUsers(groupId);
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/group/list", method = RequestMethod.GET)
	public String listUsergroup(String uid, Model model) {
		if (EmptyUtil.isNotEmpty("uid")) {
			model.addAttribute("uid", uid);
		}
		return "user/group/list";
	}

	@RequestMapping(value = "/group/list", method = RequestMethod.POST)
	@ResponseBody
	public PagerEasyUI<UserGroupDTO> listUsergroup(HttpServletRequest request, String uid, int page, int rows, String sort, String order) {
		try {
			long userid = Long.parseLong(uid);
			return userGroupService.listByEasyUI(1, userid, page, rows, sort, order);
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/group/delete", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj deleteUsergroup(HttpServletRequest request, String ids) {
		try {
			return userGroupService.deletes(ids);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/group/add", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj addUsergroup(HttpServletRequest request, UserGroup userGroup) {
		try {
			User user = (User) request.getSession().getAttribute("loginUser");
			if (user == null) {
				return new AjaxObj(0, "当前用户信息获取失败!");
			}
			userGroup.setCreateName(user.getUsername());
			userGroup.setCreateTime(DateFormat.dateToString(new Date()));
			return userGroupService.add(userGroup);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/role/list", method = RequestMethod.GET)
	public String listUserrole(String uid, Model model) {
		if (EmptyUtil.isNotEmpty("uid")) {
			model.addAttribute("uid", uid);
		}
		return "user/role/list";
	}

	@RequestMapping(value = "/role/list", method = RequestMethod.POST)
	@ResponseBody
	public PagerEasyUI<UserRoleDTO> listUserrole(HttpServletRequest request, String uid, int page, int rows, String sort, String order) {
		try {
			long userid = Long.parseLong(uid);
			return userRoleService.listByEasyUI(1, userid, page, rows, sort, order);
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/role/delete", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj deleteUserrole(HttpServletRequest request, String ids) {
		try {
			return userRoleService.deletes(ids);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/role/add", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj addUserrole(HttpServletRequest request, UserRole userRole) {
		try {
			User user = (User) request.getSession().getAttribute("loginUser");
			if (user == null) {
				return new AjaxObj(0, "当前用户信息获取失败!");
			}
			userRole.setCreateName(user.getUsername());
			userRole.setCreateTime(DateFormat.dateToString(new Date()));
			return userRoleService.add(userRole);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

}